﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Design.GridControl
{

    internal partial class GridControlEntityMemberForm : Marmat.Forms.Form
    {
        public GridControlEntityMemberForm(System.Windows.Forms.Control control)
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            List<Type> typeCollection = new List<Type>();
            foreach (System.Reflection.Assembly assembly in Marmat.Forms.DesignerReferenceAssembly.GetAssemblyCollection())
            {
                try
                {
                    foreach (Type type in assembly.GetExportedTypes())
                    {
                        if (type.IsPublic && type.IsAbstract == false)
                            typeCollection.Add(type);
                    }
                }
                catch (Exception) { }
            }
            this.gridControl1.DataSource = typeCollection;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.dataGrid1.Rows.Clear();
            if (e.FocusedRowHandle >= 0)
            {
                Type type = this.gridView1.GetRow(e.FocusedRowHandle) as Type;
                MemberInfo[] members = type.GetMembers();
                foreach (var member in members)
                {
                    Type dataType;
                    if (member is FieldInfo field)
                        dataType = field.FieldType;
                    else if (member is PropertyInfo property)
                        dataType = property.PropertyType;
                    else
                        continue;
                    var row = this.dataGrid1.AddNew();
                    row["Name"] = member.Name;
                    row["DataType"] = dataType;
                    DescriptionAttribute description = member.GetCustomAttribute<DescriptionAttribute>();
                    if (description != null)
                        row["Caption"] = description.Description;
                }
            }
            this.dataGrid1.AcceptChanges();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.dataGrid1.Rows)
            {
                row["Check"] = true;
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.dataGrid1.Rows)
            {
                row["Check"] = !(bool)row["Check"];
            }
        }
    }
}
